﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Citi.HeadCount.Domain;
using System.ComponentModel.DataAnnotations;

namespace Citi.HeadCount.WebUI.Models
{
    public class GOCAttributesViewModel 
    {
        public string GOCAttribute { get; set; }
        public string GOCAsNumber { get; set; }
        public string LongDescription { get; set; }
        public string CorpCode { get; set; }
        public string ExpenseCode { get; set; }
        public string Status { get; set; }
        public string UpdatedDate { get; set; }
        public string SID { get; set; }
        public string LocalCostCode { get; set; }
        public string ManagerId { get; set; }
        public string FRSBU { get; set; }
        public string FRSBULongDescription { get; set; }
        public string FRSOU { get; set; }
        public string FRSOULongDescription { get; set; }
        public string Function { get; set; }
        public string FunctionShortDescription { get; set; }
        public string LVID { get; set; }
        public string LVIDLongDescription { get; set; }
        public string ManagedGeography { get; set; }
        public string ManagedGeographyDescription { get; set; }
        public string ManagedSegmentId { get; set; }
        public string ManagedSegmentLongDescription { get; set; }
        public List<string> ManagedSegmentLevels { get; set; }
        public List<string> FunctionsLevels { get; set; }
        [Display(Name = "O&T Flag")]
        public string OTFlag { get; set; }
        public string Business { get; set; }
        [Display(Name = "Business 2")]
        public string Business1 { get; set; }
        public string Area { get; set; }
        [Display(Name = "Deck Regional 1")]
        public string DeckRegional { get; set; }
        [Display(Name = "Deck Regional 2")]
        public string DeckRegional1 { get; set; }
        public string Local { get; set; }
        [Display(Name = "FP&A Manager")]
        public string FPAManager { get; set; }
        [Display(Name = "FP&A Analyst")]
        public string FPAAnalyst { get; set; }
        public int Id { get; set; }

        private static void GetManagedSegmentLevels(ManagedSegment managedSegment, ref List<string> managedSegmentLevels)
        {
            managedSegmentLevels.Insert(0, managedSegment.Description);

            if (managedSegment.ParentId != null)
                GetManagedSegmentLevels(managedSegment.Parent, ref managedSegmentLevels);
        }

        private static void GetFunctionLevels(Function function, ref List<string> functionLevels)
        {
            functionLevels.Insert(0, function.Description);

            if (function.ParentId != null)
                GetFunctionLevels(function.Parent, ref functionLevels);
        }

        public static GOCAttributesViewModel CreateModel(GOC domain)
        {
            var gocAsNumber = 0;
            var gocAsString = domain.GOCAttribute;
            var gocAttributes = int.TryParse(gocAsString, out gocAsNumber) ? gocAsNumber.ToString() : gocAsString;

            var managedSegmentLevels = new List<string>();
            var functionLevels = new List<string>();

            GetFunctionLevels(domain.Function, ref functionLevels);
            GetManagedSegmentLevels(domain.ManagedSegment, ref managedSegmentLevels);

            var report = new GOCAttributesViewModel
            {
              
                Id = domain.Id,
                GOCAttribute = gocAsString,
                GOCAsNumber = gocAttributes,
                LongDescription = domain.LongDescription,
                CorpCode = string.IsNullOrEmpty(domain.CorpCode) ? "-" : domain.CorpCode,
                ExpenseCode = string.IsNullOrEmpty(domain.ExpenseCode) ? "-" : domain.ExpenseCode,
                Status = domain.Status,
                UpdatedDate = domain.UpdatedDate.ToString(),
                SID = domain.Sid.Description,
                LocalCostCode = domain.LocalCostCode,
                ManagerId = domain.Manager,
                FRSBU = domain.FRSBu.Code,
                FRSBULongDescription = domain.FRSBu.Description,
                FRSOU = domain.FRSOu.Code,
                FRSOULongDescription = domain.FRSOu.Description,
                Function = domain.Function.Code,
                FunctionShortDescription = domain.Function.Description,
                LVID = domain.Level.Code,
                LVIDLongDescription = domain.Level.Description,
                ManagedGeography = domain.Country.Abbreviation,
                ManagedGeographyDescription = domain.Country.Description,
                ManagedSegmentId = domain.ManagedSegment.Code,
                ManagedSegmentLongDescription = domain.ManagedSegment.Description,
                ManagedSegmentLevels = managedSegmentLevels,
                FunctionsLevels = functionLevels
            };

            if (domain.RuleResult == null || domain.RuleResult.TypeRuleResults == null) return report;
            GetRuleResults(domain, report);

            return report;
        }

        private static void GetRuleResults(GOC domain, GOCAttributesViewModel report)
        {
            var typeRuleResults = domain.RuleResult.TypeRuleResults;

            report.Area = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Area) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Area).TypeResult.Value : "-";

            report.Business = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Business) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Business).TypeResult.Value : "-";

            report.Business1 = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Business1) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Business1).TypeResult.Value : "-";

            report.DeckRegional = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.DeckRegional) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.DeckRegional).TypeResult.Value : "-";

            report.DeckRegional1 = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.DeckRegional1) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.DeckRegional1).TypeResult.Value : "-";

            report.FPAAnalyst = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.FPAAnalyst) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.FPAAnalyst).TypeResult.Value : "-";

            report.FPAManager = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.FPAManager) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.FPAManager).TypeResult.Value : "-";

            report.Local = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Local) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.Local).TypeResult.Value : "-";

            report.OTFlag = typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.OTFlag) != null ?
                typeRuleResults.FirstOrDefault(x => x.TypeResult.Type == TypeResultType.OTFlag).TypeResult.Value : "-";
        }

        public static IEnumerable<GOCAttributesViewModel> CreateModel(IEnumerable<GOC> domains)
        {
            return domains.Select(CreateModel);
        }
    }
}
